package trabalho.das.fxml.views.uicomponents;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import trabalho.das.fxml.views.controladores.ControladorCodeEditor;

public class CodeEditor extends BorderPane {

    private ControladorCodeEditor controladorCodeEditor = null;

    public CodeEditor() {
        controladorCodeEditor = new ControladorCodeEditor(this);
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/resources/fxml/CodeEditor.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(controladorCodeEditor);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    public void initialize(ListView<Path> javaListView, ListView<Path> classListView, TextArea codeTextArea) {
        loadJavaFiles(javaListView, codeTextArea);
        loadClassFiles(classListView);
    }

    public void loadJavaFiles(ListView<Path> javaListView, TextArea codeTextArea) {
        Path currentRelativePath = Paths.get("estrategias/");
        try {
            List<Path> collect = Files.list(currentRelativePath).filter((Path t) -> {
                return t.toString().toLowerCase().endsWith(".java");
            }).collect(Collectors.toList());
            javaListView.setItems(FXCollections.observableList(collect));

            javaListView.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends Path> observable, Path oldValue, Path newValue) -> {
                if (newValue != null) {
                    readFileToTextArea(newValue, codeTextArea);
                }
            });

        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

    public void loadClassFiles(ListView<Path> classListView) {
        Path currentRelativePath = Paths.get("");
        try {
            List<Path> collect = Files.list(currentRelativePath).filter((Path t) -> {
                return t.toString().toLowerCase().endsWith(".class");
            }).collect(Collectors.toList());
            classListView.setItems(FXCollections.observableList(collect));
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

    private void readFileToTextArea(Path path, TextArea codeTextArea) {
        File f = path.toFile();
        if (!f.exists()) {
            System.out.println(path + " não foi encontrado!");
        }
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)))) {
            StringBuilder sb = new StringBuilder();
            br.lines().forEach((String s) -> sb.append(s).append('\n'));
            codeTextArea.setText(sb.toString());
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
